﻿using AutoMapper;
using HamcoWcs.Entitys.RoleInfos;
using System.Collections.Generic;
using System.Linq;

namespace HamcoWcs.Models.ForeEndModels.RoleInfos
{
    public class RoleInfoProfile : Profile
    {
        public RoleInfoProfile()
        {
            CreateMap<RoleInfo, RoleInfoDto>()
                 .ConvertUsing(src =>
                 new RoleInfoDto()
                 {
                     Id = src.Id,
                     RoleName = src.RoleName,
                     Remark = src.Remark,
                     Menus = ConvertToList(src.Menus)
                 });

            CreateMap<RoleInfoDto, RoleInfo>()
                .ConvertUsing(src => new RoleInfo()
                {
                    Id = src.Id,
                    RoleName = src.RoleName,
                    Remark = src.Remark,
                    Menus = ConvertToString(src.Menus)
                });
        }

        private static string ConvertToString(List<long> menus)
        {
            return menus?.Count > 0 ? string.Join("|", menus) : "";
        }

        private static List<long> ConvertToList(string src)
        {
            return !string.IsNullOrEmpty(src) ? src.Split('|').Select(d => long.Parse(d)).ToList() : new List<long>();
        }
    }



}
